//
//  YQChatCell.swift
//  yq微信
//
//  Created by 杨庆 on 2018/1/12.
//  Copyright © 2018年 yq. All rights reserved.
//

import UIKit

/// 聊天界面 -- 自定义cell
class YQChatCell: UITableViewCell {

    // 设置值 子控件
    // (布局模型) 类 -- 设值
    var chatFrame:YQChatFrame? {
        // 拿到值,对控件进行赋值
        didSet{
            // 对模型数据赋值
            let chat:YQChat? = chatFrame?.chat
            
            self.timeLbl.text = chat?.timeStr
            self.userIconBtn.setImage(UIImage(named:"\(String(describing: (chat?.userIconStr!)!))"), for: UIControlState.normal)
            
            // contentBtn
            self.contentBtn.setTitle(chat?.contentText!, for: UIControlState.normal)
           
            let resiImg = UIImage.yq_resizingWithImage(img: (chat?.contentTextBGImage!)!)
            self.contentBtn.setBackgroundImage(resiImg, for: UIControlState.normal)
            let resiImg2 = UIImage.yq_resizingWithImage(img: (chat?.contentTextBGHLImage!)!)
            self.contentBtn.setBackgroundImage(resiImg2, for: UIControlState.highlighted)
        }
    }
    
    // timeLbl
    var  timeLbl:UILabel = UILabel()
    // userIconBtn
    var userIconBtn:YQLongPressBtn = YQLongPressBtn()
    // contentBtn
    var contentBtn:YQLongPressBtn = YQLongPressBtn()
    
    // 创建 子控件
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = K243Color
        
        // timeLbl
        let timeLbl : UILabel = UILabel()
        timeLbl.backgroundColor = UIColor.lightGray
        timeLbl.textColor = UIColor.white
        timeLbl.layer.cornerRadius = 4 // 设置裁剪
        timeLbl.layer.masksToBounds = true // 执行裁剪
        timeLbl.textAlignment = NSTextAlignment.center
        timeLbl.font = KTimeFont
    
        // add to the subView
        self.contentView.addSubview(timeLbl)
        self.timeLbl = timeLbl
        
        // userIconBtn
        let userIconBtn : YQLongPressBtn = YQLongPressBtn()
        //TODO:用户头像长按逻辑处理
        userIconBtn.longPressCallBack { (btn ) in
            print("长按...")
        }
        
        userIconBtn.addTarget(self , action: #selector(yq_showDetailUserInfoClick), for: UIControlEvents.touchDown)
        userIconBtn.backgroundColor = UIColor.red
        
        // add to subview
        self.contentView.addSubview(userIconBtn)
        self.userIconBtn = userIconBtn
        
        // contentBtn
        let contentBtn:YQLongPressBtn = YQLongPressBtn()
        contentBtn.layer.cornerRadius = 3 // 设置
        contentBtn.layer.masksToBounds = true // 开始裁剪
        contentBtn.setTitleColor(UIColor.black, for: UIControlState.normal)
        contentBtn.titleLabel?.font = KContentTextFont
        contentBtn.titleLabel?.numberOfLines = 0 // 文字能换行
        // 内边距
        contentBtn.contentEdgeInsets = UIEdgeInsetsMake(KContentEdgeTop , KContentEdgeLeft , KContentEdgeBottom, KContentEdgeRight)
        
        //TODO: 消息内容长按逻辑处理
        contentBtn.longPressCallBack { (btn ) in
            print("长按...")
        }
        
        contentBtn.addTarget(self, action: #selector(yq_showContentTouchUpInside), for: UIControlEvents.touchDown)
        self.contentView.addSubview(contentBtn)
        self.contentBtn = contentBtn
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    // 布局 子控件
    override func layoutSubviews() {
        super.layoutSubviews()

        self.timeLbl.frame = (chatFrame?.timeFrame)!
        self.contentBtn.frame = (chatFrame?.contentFrame)!
        self.userIconBtn.frame = (chatFrame?.userIconFrame)!
    }
}

extension YQChatCell {
    // show the user's detail info
    func  yq_showDetailUserInfoClick(){
        print("点击头像--显示好友详细信息")
    }
    
    func yq_showContentTouchUpInside(){
        print("点击聊天内容--对消息体进行处理")
    }
    
}

